/*
g++ input.cpp  `sdl2-config --cflags --libs` -o input


*/

#include <iostream>
#include <string>
#include <unistd.h>


#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>


using namespace  std;

bool init();
void kill();
bool loop();
void error(const char* info);

SDL_Window* window;
SDL_Renderer *renderer;
SDL_Texture *texture, *text;
TTF_Font *font;
string input;






int main(int argc,const char* argvp[]){

    if( !init())
    {
        error("Error initing!");
        pause();
        return 1;
    }

    while( loop() ) {

        SDL_Delay( 10 );
    }

    kill();

    return 0;
}


void error(const char* info)
{
    cout << "Error " << info << " SDL: " << SDL_GetError() << endl;
    pause();
}

bool init()
{
    if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0){
        error("Error initializing SDL: ");
        return false;
    }

    if ( IMG_Init( IMG_INIT_PNG) < 0 ) {
        error("Error initializing SDL_image: ");
        return false;
    }

    if ( TTF_Init() < 0) {
        error("Error initializing SDL_ttf: ");
        return false;
    }
                                                                   
    window = SDL_CreateWindow( "Example", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN );
    if ( !window ) {
        error("Error creating window: ");
        return false;
    }

    renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED);
    if ( !renderer) {
        error("Error creating render ");
        return false;
    }

    SDL_Surface* buffer = IMG_Load( "test.png" );
    if( !buffer) {
        error("Error loading image ");
        return false;
    }

    texture = SDL_CreateTextureFromSurface( renderer, buffer);

    SDL_FreeSurface( buffer);
    buffer = NULL;
    
    if( !texture) {
        error("Error Creating texture ");
        return false;
    }

    font = TTF_OpenFont("font.ttf", 72);
    if( !font){
        error("Error TTF_OpenFont ");
        return false;
    }

    SDL_StartTextInput();


    return true;
}
void kill()
{


}

bool loop()
{
    static const unsigned char* keys = SDL_GetKeyboardState( NULL );
     
    SDL_Event e;
    SDL_Rect dest;

    SDL_SetRenderDrawColor( renderer, 255, 255, 255, 255);
    SDL_RenderClear( renderer);

    while ( SDL_PollEvent( &e) != 0)
    {
        switch ( e.type)
        {
        case SDL_QUIT:
            return false;
        case SDL_TEXTINPUT:
            input += e.text.text;
            break;
        case SDL_KEYDOWN:
            if ( e.key.keysym.sym == SDLK_BACKSPACE && input.size()){
                input.pop_back();
            }
            break;
        
        default:
            break;
        }

    }

    SDL_RenderCopy( renderer, texture, NULL, NULL);

    SDL_Color foreground = { 0, 0, 0};

    if( input.size()){ 
        SDL_Surface* text_surf = TTF_RenderText_Solid( font, input.c_str(), foreground);
        text = SDL_CreateTextureFromSurface( renderer, text_surf);

        dest.x = 320 - (text_surf->w / 2.0f);
        dest.y = 240;
        dest.w = text_surf->w;
        dest.h = text_surf->h;
        SDL_RenderCopy( renderer, text, NULL, &dest);

        SDL_DestroyTexture(text);
        SDL_FreeSurface(text_surf);
    }
    SDL_RenderPresent( renderer);

    return true;
}